﻿/* 
  Author : Nguyen Minh Triet
  Email  : nguyenminhtriet@kng-vn.com
  Date   : 03/10/2008
  Company: K&G SoftWare
  Team   : CLN
 */

using System;
using System.Text.RegularExpressions;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CBMS.Objects;
using CBMS.Business;
using System.Resources; 
using System.IO;
using System.Xml;
using CBMS.Interfaces;
using CBMS.Models;
using CBMS.Common;
using CBMS.Resources;

namespace CBMS.View
{
    public partial class frmSystemConfig : frmBase
    {
        #region Fields
        private string strError = string.Empty;
        public static ISystemConfigRepository systemConfigRep { get; set; }
        #endregion

        #region Contructor, Form_Load
        public frmSystemConfig()
        {
            systemConfigRep = new SystemConfigRepository();
            InitializeComponent();
        }
        private void frmSystemConfig_Load(object sender, EventArgs e)
        {
            LoadObjectSystemConfig();
        }

        #endregion

        #region Methods
        /// <summary>
        /// Load data from object SystemConfig
        /// </summary>
        private void LoadObjectSystemConfig()
        {
            var objItem = systemConfigRep.GetSystemConfig();
            if (objItem != null)
            {
                cmbPageSize.SelectedItem = objItem.page_size.ToString();
                txtEmailAddress.Text = objItem.email_address;
                txtEmailServerName.Text = objItem.email_server_name; 
                mTxtPort.Text = objItem.email_server_port.ToString();
            }
        }

        private void SetDataToObjectSystemConfig(ref SystemConfig objSystemConfig)
        {
            if (objSystemConfig == null)
            {
                objSystemConfig = new SystemConfig();
            }
            objSystemConfig.page_size = DBConvert.ParseInt(cmbPageSize.SelectedItem.ToString());
            objSystemConfig.email_address = txtEmailAddress.Text.Trim();
            objSystemConfig.email_server_name = txtEmailServerName.Text.Trim(); 
            objSystemConfig.email_server_port = DBConvert.ParseInt(mTxtPort.Text.Trim());
            objSystemConfig.created_by = this.userLogin;
            objSystemConfig.created_date = CommonFunction.Now;
        }

        #endregion

        #region Handle Events
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (cmbPageSize.Text.Trim() == string.Empty || cmbPageSize.Text.Trim().ToLower().Equals("---please select ---"))
            {
                strError = string.Format(ErrorMessages.E0001, BaseFunctions.Removetext(lblPageSize.Text));
                MessageBox.Show(strError, ErrorMessages.T001, MessageBoxButtons.OK, MessageBoxIcon.Error);
                cmbPageSize.Focus();
                return;
            }
            if (txtEmailAddress.Text.Trim() == string.Empty)
            {
                strError = string.Format(ErrorMessages.E0001, BaseFunctions.Removetext(lblEmailAddress.Text));
                MessageBox.Show(strError, ErrorMessages.T001, MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtEmailAddress.Focus();
                return;
            }

            if (!BaseFunctions.CheckFormatEmail(txtEmailAddress.Text.Trim()))
            {
                strError = string.Format(ErrorMessages.E0003, BaseFunctions.Removetext(lblEmailAddress.Text));
                MessageBox.Show(strError, ErrorMessages.T001, MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtEmailAddress.Focus();
                return;
            }

            if (txtEmailServerName.Text.Trim() == string.Empty)
            {
                strError = string.Format(ErrorMessages.E0001, BaseFunctions.Removetext(lblEmailServerName.Text));
                MessageBox.Show(strError, ErrorMessages.T001, MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtEmailServerName.Focus();
                return;
            } 

            if (mTxtPort.Text.Trim() == string.Empty)
            {
                strError = string.Format(ErrorMessages.E0001, BaseFunctions.Removetext(lblEmailServerPort.Text));
                MessageBox.Show(strError, ErrorMessages.T001, MessageBoxButtons.OK, MessageBoxIcon.Error);
                mTxtPort.Focus();
                return;
            }

            var objSystemConfig = systemConfigRep.GetSystemConfig();
            SetDataToObjectSystemConfig(ref objSystemConfig);
            try
            {
                if (objSystemConfig != null && objSystemConfig.config_id == 0)
                {
                    systemConfigRep.Add(objSystemConfig);
                }
                systemConfigRep.SaveChanges();
                strError = string.Format(ErrorMessages.I0001, "Update");
                MessageBox.Show(strError, ErrorMessages.T002, MessageBoxButtons.OK, MessageBoxIcon.Information);
                LoadObjectSystemConfig();
            }
            catch
            {
                strError = string.Format(ErrorMessages.I0003, "Update");
                MessageBox.Show(strError, ErrorMessages.T001, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        } 
        #endregion        
    }
}